package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    //1、新增商机
    @PostMapping
    public Result addBusiness(@RequestBody Business business) {
        log.info("新增商机");
        business.setStatus(1);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        log.info("保存数据：" + business);
        return Result.success();
    }

    //2、商机列表查询
    @GetMapping
    public Result listBusinesses(BusinessDto businessDto) {
        log.info("查询商机列表");
        //调用service方法
        PageResult<Business> pageResult = businessService.listBusinesses(businessDto);
        return Result.success(pageResult);
    }
    //3、分配商机跟进人
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机跟进人");
        //查询商机是否存在
        Business business = new Business();
        //设置值
        business.setId(businessId);
        business.setStatus(2);
        //设置归属人
        business.setUserId(userId);
        //设置更新时间
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }
    //4、根据ID查询商机详细信息
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id) {
        log.info("查询商机：{}", id);
        //调用service方法
        Business business = businessService.getBusinessById(id);
        return Result.success(business);


    }
    //5、跟进商机
    @PutMapping
    public Result trackBusiness(@RequestBody Business business) {
        log.info("跟进商机");
        businessService.trackBusiness(business);
        return Result.success();
    }
    //6、踢回公海
    @PutMapping("/back/{id}")
    public Result backToPool(@PathVariable Integer id) {
        log.info("踢回公海");
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);
        //更新时间
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();


    }
    //7、 转客户处理
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("转客户处理");
        //调用service方法
        businessService.toCustomer(id);
        //返回数据
        return Result.success();


    }
}
